package com.base.cn.platform.os.common.redis;

import com.base.cn.platform.os.common.constants.CacheConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
/**
 * @author : guo shi qi
 * @Date: 2019-08-05 17:43
 * @Description:
 */
@Component
public class RedisChannel {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 获得下一个ID
     * @param HK key
     * @param delta 每次增加值
     * @return
     */
    public synchronized  Long getNextId (String HK,Long delta){
        Long increment = redisTemplate.opsForHash().increment(CacheConstants.REDIS_NEXT_ID, HK, delta);
        if(increment<4000L){
            this.setInitId(HK,4000L);
            increment =4000L;
        }
        return increment ;
    }

    /**
     * 设置起始ID
     * @param HK key
     * @param init 初始值
     */
    public void setInitId(String HK,Long init){
        redisTemplate.opsForHash().put(CacheConstants.REDIS_NEXT_ID, HK, init);
    }
}
